<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class news extends CI_Controller {
	
	function __construct() {
			parent::__construct();
			
			$this->load->library('ion_auth');
			$this->load->library('form_validation');
			$this->load->library('session');
			$this->load->library('pagination');
			$this->load->database();
			$this->lang->load('auth');
			$this->lang->load('admin_estate');
			$this->load->helper('language');
			$this->load->model('news_model');
			if (!$this->ion_auth->logged_in()) {
				redirect('login');
			}
			$this->data['ptitle'] = 'News';
		}

	public function index(){
		$this->session->set_userdata('page', 0);
		$this->data['news'] = $this->news_model->get_news();
        $this->tmadmin->tmView('news/news', $this->data);
	}
	
	public function view(){
		$this->session->set_userdata('page', 0);
	$this->data['news_details'] = $this->_get_page($this->uri->segment(3));
	$loaded='';
	if ($this->uri->segment(3) == 'edit_news') {
		$loaded = 'edit_news';
	}elseif($this->uri->segment(3) == 'add_news'){
		$loaded = 'add_news';
	}
	$this->tmadmin->tmView('news/'.$loaded, $this->data);
	
	}
	
	
	function _get_page($status){
		switch ($status) {
			case 'edit_news':
				return $this->news_model->get_news_details($this->uri->segment(4));
				break;
				case 'edit_sub':
				return $this->news_model->get_sub_details($this->uri->segment(4));
				break;
			default:
				
			break;
		}
		
	}
	
	public function insert_news()
	{
		$exp = strtotime($this->input->post('expiry_date'));
		$data = array(
		'message' => $this->input->post('message'),
		'author' => $this->ion_auth->get_user_id(),
		'link' => $this->input->post('link'),
		'expiry_date' => $exp
		);
		$this->db->insert('news',$data);
		
		$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Record Successfully Created</p> </div>');

		
		
		redirect('news/view/add_news');
	}
	
	
	
	public function update_news()
	{
		$id = $this->input->post('news_id');
		$exp = strtotime($this->input->post('expiry_date'));
		$data = array(
		'message' => $this->input->post('message'),
		'author' => $this->ion_auth->get_user_id(),
		'link' => $this->input->post('link'),
		'expiry_date' => $exp
		);
		$this->db->where('news_id',$id);
		$this->db->update('news',$data);
		
		$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Record Successfully Updated</p> </div>');
		
		
		redirect('news/view/edit_news/'.$id);
	}
	
	
	
	
	
	public function delete_news($news_id){
		
		$this->db->where('news_id',$news_id);
		$this->db->delete('news');
		$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Record Successfully Deleted</p> </div>');
		
		redirect('news');
	}
	
	/************Subscrribers **********/
	
	public function subscribers(){
	$this->session->set_userdata('page', 14);
	$this->data['subscribers'] = $this->news_model->get_subscribers();
	$this->tmadmin->tmView('news/subscribers', $this->data);
	
	}
	
	
	public function sub(){
	$loaded ='';
	$this->data['sub_details'] = $this->_get_page($this->uri->segment(3));
	if ($this->uri->segment(3) == 'edit_sub') {
		$loaded = 'edit_sub';
	}elseif($this->uri->segment(3) == 'add_sub'){
		$loaded = 'add_sub';
	}
	$this->session->set_userdata('page', 14);
	$this->tmadmin->tmView('news/'.$loaded, $this->data);
	}
	
	
	public function insert_sub()
	{
		$data = array(
		'sub_name' => $this->input->post('sub_name'),
		'sub_email' => $this->input->post('sub_email'),
		);
		$this->db->insert('subscribers',$data);
		$this->_log_activity('added_subscriber');
		$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Add Subscribers Successfully</p> </div>');
		//$this->session->set_flashdata('success', $this->news_model->messages());
		redirect('news/sub/add_sub');
	}
	function _log_activity($message)
	{
		$this->db->set('user', $this->ion_auth->get_user_id()); 
		$this->db->set('message', $message); 
		$this->db->insert('timeline'); 
		return TRUE;
	}
	
	public function delete_sub($sub_id){
	        if (!empty($sub_id)) {
			$this->db->where('sub_id',$sub_id);
			$this->db->delete('subscribers');
			$this->_log_activity('deleted_subscriber');
			
			$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Record Successfully Deleted</p> </div>');
			
			redirect('news/subscribers');
	   }else{
		$this->session->set_flashdata('message','<div class="alert alert-error"> <p>Record not Deleted</p> </div>');
		
		redirect('news/subscribers');
	  }
		
	}
	public function update_sub(){
		$id = $this->input->post('sub_id');
		$data = array(
		'sub_name' => $this->input->post('sub_name'),
		'sub_email' => $this->input->post('sub_email')
		);
		$this->db->where('sub_id',$id);
		$this->db->update('subscribers',$data);
		$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Record Successfully Updated</p> </div>');
		redirect('news/sub/edit_sub/'.$id);
	}
	
	/*****************************Export CSV */
	
	public function export_csv(){
		$this->load->dbutil();
		$this->load->helper('file');
		$this->db->select('sub_name,sub_email');
		$query = $this->db->get('subscribers');
		$data = $this->dbutil->csv_from_result($query);
		$name = 'subscribers_'.date ( 'dMy' ) .'.csv';
		if ( ! write_file('./assets/backups/'.$name, $data))
		{
		$this->session->set_flashdata('message','<div class="alert alert-error"> <p>Failed Write</p> </div>');
		
		redirect('news/subscribers');
		}
		else
			{
			$this->_log_activity($this->lang->line('exported_csv'));
			$this->load->helper('download');
			force_download($name, $data);
			
			$this->session->set_flashdata('message','<div class="alert alert-success"> <p>Subscribers Exported</p> </div>');
			redirect('news/subscribers');
			}
		
	}
	
	
	
}

/* End of file news.php */